<?php
class Tourismheadline extends  CS_Controller
{
    public function _init() {
        $this->load->library('pagination');
        $this->load->model('tourism_headline_model','tourism_headline');
        $this->load->model('tourism_headline_topic_model','tourism_headline_topic');
    }
    
    public function grid($pg=1){
        $getData = $this->input->get();
        $getData['provider_id'] = $this->uid;
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('tourismheadline/grid').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismheadline/grid');
        $config['total_rows'] = $this->tourism_headline->findByCount($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_headline->pagelist($page_num,$num,$getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $this->load->view('tourismheadline/grid', $data);
    }
    
    public function add(){
        $data['topic'] = $this->tourism_headline_topic->findAll(['status'=>1],'id,name');
        $this->load->view('tourismheadline/add',$data);
    }
    
    public function addPost(){
        $this->validate();
        $data = $this->input->post();
        $data['provider_id'] = $this->uid;
        $data['created_at'] = date('Y-m-d H:i:s');
        $state = $this->tourism_headline->insert($data);
        if (!$state){
            $this->ajaxJson(['status' => false, 'msg' => '添加失败']);
        }
        $this->ajaxJson(['status' => true, 'msg' => site_url('tourismheadline/grid')]);
    }
    public function edit($id){
        $res = $this->tourism_headline->findOne(['id'=>$id,'provider_id'=>$this->uid]);
        if ($res->num_rows() <= 0){
            $this->error('tourismheadline/grid', '', '该公告不存在');
        }
        $data['headline'] = $res->row();
        $data['topic'] = $this->tourism_headline_topic->findAll(['status'=>1],'id,name');
        $this->load->view('tourismheadline/edit',$data);
    }
    
    public function editPost(){
        $this->validate();
        $res = $this->tourism_headline->findOne(['id'=>$this->input->post('id'),'provider_id'=>$this->uid]);
        if ($res->num_rows() <= 0){
            $this->ajaxJson(['status' => false, 'msg' => '该公告不存在']);
        }
        $data = $this->input->post();
        unset($data['id']);
        $data['updated_at'] = date('Y-m-d H:i:s');
        $state = $this->tourism_headline->update($data,['id'=>$this->input->post('id'),'provider_id'=>$this->uid]);
        if (!$state){
            $this->ajaxJson(['status' => false, 'msg' => '编辑失败']);
        }
        $this->ajaxJson(['status' => true, 'msg' => site_url('tourismheadline/grid')]);
    }
    
    public function delete($id){
        $res = $this->tourism_headline->findOne(['id'=>$id,'provider_id'=>$this->uid]);
        if ($res->num_rows() <= 0){
            $this->error('tourismheadline/grid', '', '该公告不存在');
        }
        $state = $this->tourism_headline->delete($id);
        if (!$state){
            $this->error('tourismheadline/grid', '', '删除失败');
        }
        $this->success('tourismheadline/grid', '', '删除成功');
    }
    
    public function validate(){
        if ($this->validateParam($this->input->post('topic_id'))){
            $this->ajaxJson(['status' => false, 'msg' => '主题不能为空']);
        }
        if ($this->validateParam($this->input->post('title'))){
            $this->ajaxJson(['status' => false, 'msg' => '标题不能为空']);
        }
        if ($this->validateParam($this->input->post('content'))){
            $this->ajaxJson(['status' => false, 'msg' => '内容不能为空']);
        }
        if ($this->validateParam($this->input->post('sort'))){
            $this->ajaxJson(['status' => false, 'msg' => '主题不能为空']);
        }
        if (!is_numeric($this->input->post('sort'))){
            $this->ajaxJson(['status' => false, 'msg' => '排序必须为数字']);
        }
    }
}